{% extends "base.html" %}

{% block title %}交通指南 - 风之宿{% endblock %}

{% block content %}
{# 页面头部：添加page_contents存在性检查 #}
<section class="py-16 bg-primary/10">
  <div class="container mx-auto px-4 sm:px-6 lg:px-8">
    <div class="text-center max-w-3xl mx-auto">
      <h1 class="text-4xl sm:text-5xl font-serif font-bold text-primary mb-4">交通指南</h1>
      <p class="text-xl text-gray-600">
        {# 先检查page_contents是否存在，再访问其属性 #}
        {% if page_contents and page_contents.description %}{{ page_contents.description }}{% else %}风之宿交通便利，从主要交通枢纽均可轻松到达。我们提供详细的交通指南和地图，帮助您顺利找到我们的位置。{% endif %}
      </p>
    </div>
  </div>
</section>

{# 地图模块：添加page_contents默认值保护 #}
<section class="py-16 bg-white">
  <div class="container mx-auto px-4 sm:px-6 lg:px-8">
    <h2 class="text-3xl font-serif font-bold text-primary text-center mb-12">地理位置</h2>

    <div class="bg-gray-100 rounded-lg overflow-hidden shadow-lg mb-8">
      <div id="map-container" class="relative h-[400px] sm:h-[480px]">
        <div id="map-loading" class="absolute inset-0 flex items-center justify-center bg-white/80 z-10">
          <div class="flex flex-col items-center">
            <i class="fa fa-spinner fa-spin text-primary text-2xl mb-2"></i>
            <span class="text-gray-600">地图加载中...</span>
          </div>
        </div>

        {# 使用字典默认值处理page_contents未定义的情况 #}
        <iframe
          src="{{ ('https://map.baidu.com/@13203488.98245225,3691246.0849829433,14z') }}"
          width="100%"
          height="100%"
          style="border:0;"
          allowfullscreen=""
          loading="lazy"
          referrerpolicy="no-referrer-when-downgrade"
          onload="document.getElementById('map-loading').classList.add('hidden')">
        </iframe>
      </div>
    </div>

    <div class="text-center">
      <a
        href="{% if page_contents %}{{ page_contents.map_link }}{% else %}https://map.baidu.com/@13203488.98245225,3691246.0849829433,14z{% endif %}"
        target="_blank"
        rel="noopener noreferrer"
        class="inline-flex items-center text-accent hover:text-primary transition-colors duration-300 font-medium text-lg px-4 py-2 rounded-md hover:bg-primary/5">
        <i class="fa fa-map-marker mr-2"></i>
        在Google地图上查看
        <i class="fa fa-external-link ml-2 text-sm"></i>
      </a>
    </div>
  </div>
</section>

{# 交通方式：处理page_contents可能未定义的情况 #}
<section class="py-16 bg-neutral">
  <div class="container mx-auto px-4 sm:px-6 lg:px-8">
    <h2 class="text-3xl font-serif font-bold text-primary text-center mb-12">交通方式</h2>

    {# 先检查page_contents是否存在，再获取transport_modes #}
    {% if page_contents and page_contents.transport_modes %}
      {% set transport_modes = page_contents.transport_modes %}
    {% else %}
      {% set transport_modes = [
        {
          "icon": "train",
          "title": "铁路",
          "desc": "从JR○○站步行约5分钟即可到达。<br>从东京站乘坐新干线约2小时。"
        },
        {
          "icon": "bus",
          "title": "巴士",
          "desc": "从○○机场乘坐直达巴士约60分钟。<br>从○○巴士总站乘坐市营巴士约20分钟。"
        },
        {
          "icon": "car",
          "title": "自驾",
          "desc": "从○○高速公路出口约10分钟车程。<br>提供免费停车场。"
        },
        {
          "icon": "taxi",
          "title": "出租车",
          "desc": "从JR○○站乘坐出租车约5分钟。<br>从○○机场乘坐出租车约50分钟。"
        }
      ] %}
    {% endif %}

    <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-6 sm:gap-8">
      {% for mode in transport_modes %}
        <div class="text-center p-4 rounded-lg hover:bg-white hover:shadow-md transition-all duration-300">
          <div class="w-20 h-20 bg-primary rounded-full flex items-center justify-center mx-auto mb-4">
            <i class="fa fa-{{ mode.icon }} text-white text-2xl"></i>
          </div>
          <h3 class="text-xl font-semibold text-primary mb-3">{{ mode.title }}</h3>
          <p class="text-gray-600 leading-relaxed">
            {{ mode.desc | safe }}
          </p>
        </div>
      {% endfor %}
    </div>
  </div>
</section>

{# 详细路线：处理page_contents可能未定义的情况 #}
<section class="py-16 bg-white">
  <div class="container mx-auto px-4 sm:px-6 lg:px-8">
    <h2 class="text-3xl font-serif font-bold text-primary text-center mb-12">详细路线</h2>

    <div class="max-w-4xl mx-auto">
      <div class="space-y-8">
        {# 路线1：JR站出发 #}
        <article class="bg-neutral rounded-lg p-6 sm:p-8">
          <h3 class="text-2xl font-semibold text-primary mb-6 flex items-center">
            <i class="fa fa-subway mr-3 text-lg"></i>
            {# 安全获取main_station属性 #}
            从JR{% if page_contents and page_contents.main_station %}{{ page_contents.main_station }}{% else %}○○{% endif %}站出发
          </h3>

          <div class="space-y-6">
            {% set jr_steps = [
              "从车站东口出站",
              "沿着站前大道直行约200米",
              "在第一个路口左转，进入樱花通",
              "继续直行约300米，风之宿在右侧"
            ] %}

            {% for step in jr_steps %}
              <div class="flex items-start">
                <div class="w-8 h-8 bg-primary rounded-full flex items-center justify-center text-white font-bold mr-4 flex-shrink-0 mt-0.5">
                  {{ loop.index }}
                </div>
                <p class="text-gray-700 text-lg leading-relaxed">{{ step }}</p>
              </div>
            {% endfor %}
          </div>
        </article>

        {# 路线2：高速公路出发 #}
        <article class="bg-neutral rounded-lg p-6 sm:p-8">
          <h3 class="text-2xl font-semibold text-primary mb-6 flex items-center">
            <i class="fa fa-road mr-3 text-lg"></i>
            {# 安全获取highway属性 #}
            从{% if page_contents and page_contents.highway %}{{ page_contents.highway }}{% else %}○○{% endif %}高速公路出发
          </h3>

          <div class="space-y-6">
            {% set highway_steps = [
              "从○○高速公路○○出口下高速",
              "沿国道123号行驶约3公里",
              "看到风之宿指示牌后右转",
              "沿小路行驶约500米即到"
            ] %}

            {% for step in highway_steps %}
              <div class="flex items-start">
                <div class="w-8 h-8 bg-primary rounded-full flex items-center justify-center text-white font-bold mr-4 flex-shrink-0 mt-0.5">
                  {{ loop.index }}
                </div>
                <p class="text-gray-700 text-lg leading-relaxed">{{ step }}</p>
              </div>
            {% endfor %}
          </div>
        </article>
      </div>
    </div>
  </div>
</section>

{# 接送服务：处理page_contents可能未定义的情况 #}
<section class="py-16 bg-neutral">
  <div class="container mx-auto px-4 sm:px-6 lg:px-8">
    <h2 class="text-3xl font-serif font-bold text-primary text-center mb-12">接送服务</h2>

    <div class="max-w-4xl mx-auto">
      <div class="bg-white rounded-lg p-6 sm:p-8 shadow-sm">
        <div class="grid grid-cols-1 md:grid-cols-2 gap-6 sm:gap-8">
          {# 免费接送 #}
          <div class="p-4 border border-primary/20 rounded-lg">
            <h3 class="text-xl font-semibold text-primary mb-4 flex items-center">
              <i class="fa fa-check-circle mr-2 text-green-500"></i>
              免费接送
            </h3>
            <p class="text-gray-600 mb-4 leading-relaxed">
              我们为入住客人提供JR{% if page_contents and page_contents.main_station %}{{ page_contents.main_station }}{% else %}○○{% endif %}站的免费接送服务，让您的旅程更加轻松便捷。
            </p>
            <ul class="space-y-2 text-gray-600">
              {% for item in ["需提前1天预约", "服务时间：8:00 - 20:00", "最多可容纳4人及行李"] %}
                <li class="flex items-center">
                  <i class="fa fa-check text-primary mr-2"></i>
                  {{ item }}
                </li>
              {% endfor %}
            </ul>
          </div>

          {# 付费接送 #}
          <div class="p-4 border border-primary/20 rounded-lg">
            <h3 class="text-xl font-semibold text-primary mb-4 flex items-center">
              <i class="fa fa-money mr-2 text-blue-500"></i>
              付费接送
            </h3>
            <p class="text-gray-600 mb-4 leading-relaxed">
              如需从机场或其他地点接送，我们提供付费接送服务，价格合理，舒适安全。
            </p>
            <ul class="space-y-2 text-gray-600">
              {% for item in ["○○机场：¥5,000", "○○车站：¥2,000", "需提前预约"] %}
                <li class="flex items-center">
                  <i class="fa fa-check text-primary mr-2"></i>
                  {{ item }}
                </li>
              {% endfor %}
            </ul>
          </div>
        </div>

        {# 预约电话：安全获取reserve_tel #}
        <div class="mt-8 p-4 sm:p-6 bg-primary/10 rounded-lg">
          <p class="text-center text-gray-700 text-lg">
            <i class="fa fa-phone text-primary mr-2"></i>
            接送服务预约：
            <a
              href="tel:{% if page_contents and page_contents.reserve_tel %}{{ page_contents.reserve_tel }}{% else %}0123-45-6789{% endif %}"
              class="text-primary font-medium hover:underline transition-colors duration-300"
            >
              {% if page_contents and page_contents.reserve_tel %}{{ page_contents.reserve_tel }}{% else %}0123-45-6789{% endif %}
            </a>
            <span class="block text-sm mt-1 text-gray-600">（每日 8:00 - 22:00 可预约）</span>
          </p>
        </div>
      </div>
    </div>
  </div>
</section>

{# 周边信息：保持不变 #}
<section class="py-16 bg-white">
  <div class="container mx-auto px-4 sm:px-6 lg:px-8">
    <h2 class="text-3xl font-serif font-bold text-primary text-center mb-12">周边信息</h2>

    {% set around_facilities = [
      {
        "icon": "shopping-bag",
        "title": "便利店",
        "desc": "步行3分钟即有24小时便利店"
      },
      {
        "icon": "utensils",
        "title": "餐厅",
        "desc": "周边有多家当地特色餐厅"
      },
      {
        "icon": "landmark",
        "title": "景点",
        "desc": "距离著名景点○○仅10分钟车程"
      },
      {
        "icon": "bath",
        "title": "温泉",
        "desc": "步行15分钟可达当地温泉浴场"
      },
      {
        "icon": "park",
        "title": "公园",
        "desc": "附近有城市公园，适合散步休闲"
      },
      {
        "icon": "store",
        "title": "特产店",
        "desc": "车站周边有多家特产商店"
      }
    ] %}

    <div class="grid grid-cols-2 sm:grid-cols-3 lg:grid-cols-6 gap-6 sm:gap-8">
      {% for facility in around_facilities %}
        <div class="text-center p-4 rounded-lg hover:bg-neutral transition-all duration-300">
          <div class="w-16 h-16 bg-primary rounded-full flex items-center justify-center mx-auto mb-4">
            <i class="fa fa-{{ facility.icon }} text-white text-xl"></i>
          </div>
          <h3 class="text-lg font-semibold text-primary mb-2">{{ facility.title }}</h3>
          <p class="text-gray-600 text-sm leading-relaxed">{{ facility.desc }}</p>
        </div>
      {% endfor %}
    </div>
  </div>
</section>

{# 帮助模块：处理page_contents可能未定义的情况 #}
<section class="py-16 bg-primary text-white">
  <div class="container mx-auto px-4 sm:px-6 lg:px-8 text-center">
    <h2 class="text-3xl sm:text-4xl font-serif font-bold mb-6">需要帮助？</h2>
    <p class="text-xl mb-8 max-w-2xl mx-auto leading-relaxed">
      如果您在前往风之宿的途中需要任何帮助，请随时联系我们，我们将为您提供一对一指引。
    </p>

    <div class="max-w-2xl mx-auto bg-white/10 rounded-lg p-6 sm:p-8 backdrop-blur-sm">
      <div class="grid grid-cols-1 sm:grid-cols-2 gap-6 text-left">
        <div class="space-y-2">
          <h4 class="font-semibold text-lg mb-2 flex items-center">
            <i class="fa fa-map-marker mr-2"></i>
            地址
          </h4>
          <p class="text-white/90">
            {# 安全获取address属性 #}
            {% if page_contents and page_contents.address %}{{ page_contents.address|safe }}{% else %}〒123-4567<br>○○县○○市○○町1-2-3{% endif %}
          </p>
        </div>
        <div class="space-y-2">
          <h4 class="font-semibold text-lg mb-2 flex items-center">
            <i class="fa fa-phone mr-2"></i>
            联系电话
          </h4>
          <p class="text-white/90">
            <a href="tel:{% if page_contents and page_contents.contact_tel %}{{ page_contents.contact_tel }}{% else %}0123-45-6789{% endif %}" class="hover:underline">
              {% if page_contents and page_contents.contact_tel %}{{ page_contents.contact_tel }}{% else %}0123-45-6789{% endif %}
            </a>
          </p>
        </div>
      </div>
    </div>
  </div>
</section>

{% block extra_js %}
<script>
  // 地图加载失败降级处理
  document.querySelector('iframe[src*="map"]').addEventListener('error', function() {
    const mapContainer = document.getElementById('map-container');
    // 安全获取地址信息
    const address = "{% if page_contents and page_contents.address %}{{ page_contents.address }}{% else %}〒123-4567 ○○县○○市○○町1-2-3{% endif %}";
    mapContainer.innerHTML = `
      <div class="absolute inset-0 flex items-center justify-center bg-white/90">
        <div class="flex flex-col items-center text-center p-4">
          <i class="fa fa-map-o text-primary text-3xl mb-3"></i>
          <h4 class="text-lg font-semibold text-gray-800 mb-2">地图加载失败</h4>
          <p class="text-gray-600 max-w-sm">
            您可以通过地址导航：${address}
          </p>
        </div>
      </div>
    `;
  });
</script>
{% endblock %}
{% endblock %}
